﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuickDev.Common.Audit;
using QuickDev.Common.Utils;
using QuickDev.Localization;

using QuickDev.Model;
using QuickDev.Caching;
using QuickDev.BLL;
using QuickDev.Web.UI;
using QuickDev.Services;

#endregion

namespace QuickDev.Web.App.Administration
{
    public partial class ReturnOrderList : AdminPage
    {
        #region Member Variables

        string view = string.Empty;
        string searchTerms = string.Empty;
        int orderStatusId = 0;

        #endregion

        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load( object sender , EventArgs e )
        {
            try
            {
                view = Utility.GetParameter( "view" );
                searchTerms = Utility.GetParameter( "searchTerms" );
                orderStatusId = Utility.GetIntParameter( "orderStatusId" );

                SetProperties();

                if ( !IsPostBack )
                {

                    LoadReturnOrderStatuses();

                    LoadReturnOrders( ReturnOrderHeaderCache.GetList( orderStatusId ) );

                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnOrderList ).Name + ".Page_Load" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        ///// <summary>
        ///// Handles the Click event of the btnSearch control.
        ///// </summary>
        ///// <param name="sender">The source of the event.</param>
        ///// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        //protected void btnSearch_Click( object sender , EventArgs e )
        //{
        //    try
        //    {
        //        DateTime startDate = (DateTime)calendarStartDate.SelectedDate;
        //        DateTime endDate = ( (DateTime)calendarEndDate.SelectedDate ).AddDays( 1 ).AddSeconds( -1 );

        //        //if ( String.IsNullOrEmpty( searchTerms ) )
        //        //{
        //        //    LoadReturnOrders( ReturnOrderCache.GetList( orderStatusId , startDate , endDate ) );
        //        //}
        //        //else
        //        //{
        //        //    LoadReturnOrders( ReturnOrderCache.Search( orderStatusId , searchTerms , startDate , endDate ) );
        //        //}
        //    }
        //    catch ( System.Threading.ThreadAbortException )
        //    {
        //        //ignore error.
        //    }
        //    catch ( Exception ex )
        //    {
        //        Logger.Error( typeof( ReturnOrderList ).Name + ".btnSearch_Click" , ex );
        //        Master.MessageCenter.DisplayCriticalMessage( ex.Message );
        //    }
        //}

        /// <summary>
        /// Handles the PageIndexChanging event of the dgReturnOrders control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridPageChangedEventArgs"/> instance containing the event data.</param>
        protected void dgReturnOrders_PageIndexChanging( object sender , DataGridPageChangedEventArgs e )
        {

            LoadReturnOrders( ReturnOrderHeaderCache.GetList( orderStatusId ) );

            dgReturnOrders.CurrentPageIndex = e.NewPageIndex;
            dgReturnOrders.DataBind();
        }


        #endregion

        #region Methods

        #region Protected

        /// <summary>
        /// Formats the ReturnOrder Edit Url.
        /// </summary>
        /// <param name="sReturnOrderId">The ReturnOrder Id.</param>
        /// <returns>string ReturnOrderEdit Url</returns>
        protected string FormatEditUrl( string sReturnOrderId )
        {
            return string.Format( "ReturnOrderEdit.aspx?orderId={0}" , sReturnOrderId );
        }

        /// <summary>
        /// Formats the ReturnOrderItem Name.
        /// </summary>
        /// <param name="sReturnOrderItemId">The ReturnOrderItem Id.</param>
        /// <returns>string ReturnOrderItem Name</returns>
        protected string FormatGetVoucherItemName( string sReturnOrderItemId )
        {
            //int orderItemId = 0;
            //int.TryParse( sReturnOrderItemId , out orderItemId );

            //if ( orderItemId > 0 )
            //{
            //    return VoucherItemCache.GetModel( orderItemId ).Name;
            //}

            return LocalizationUtility.GetText( "lblUnKnown" );
        }

        /// <summary>
        /// Formats the ReturnOrderSummary Name.
        /// </summary>
        /// <param name="sReturnOrderSummaryId">The ReturnOrderSummary Id.</param>
        /// <returns>string ReturnOrderSummary Name</returns>
        protected string FormatGetVoucherSummaryName( string sReturnOrderSummaryId )
        {
            //int orderSummaryId = 0;
            //int.TryParse( sReturnOrderSummaryId , out orderSummaryId );

            //if ( orderSummaryId > 0 )
            //{
            //    return VoucherSummaryCache.GetModel( orderSummaryId ).Name;
            //}

            return LocalizationUtility.GetText( "lblUnKnown" );
        }

        /// <summary>
        /// Formats the orderAccount.
        /// </summary>
        /// <param name="sReturnOrderAccount">sReturnOrderAccount</param>
        /// <returns>string</returns>
        protected string FecthReturnOrderAccount( string sReturnOrderAccount )
        {
            return ServiceUtility.GetFormattedAmount( sReturnOrderAccount , true );
        }

        /// <summary>
        /// SetMember
        /// </summary>
        /// <param name="member">member</param>
        /// <returns></returns>
        protected string SetMember( string member )
        {
            if ( String.IsNullOrEmpty( member ) )
            {
                return member;
            }
            return Strings.Highlight( searchTerms , member );
        }


       

        /// <summary>
        /// Formats the ReturnOrderStatus Name.
        /// </summary>
        /// <param name="sReturnOrderStatusId">The ReturnOrderStatus Id.</param>
        /// <returns>string ReturnOrderStatus Name</returns>
        protected string GetFormattedReturnOrderStatusName( string sReturnOrderStatusId )
        {
            int orderStatusId = 0;
            int.TryParse( sReturnOrderStatusId , out orderStatusId );

            if ( orderStatusId > 0 )
            {
                return ServiceUtility.GetFormattedStatus( orderStatusId , ReturnOrderStatusCache.GetModel( orderStatusId ).Name );
            }

            return LocalizationUtility.GetText( "lblUnKnown" );
        }

        /// <summary>
        /// Formats the Employee Name.
        /// </summary>
        /// <param name="sEmployeeId">The Employee Id.</param>
        /// <returns>string Employee Name</returns>
        protected string FormatGetEmployeeName( string sEmployeeId )
        {
            return WebUtility.FecthUpdateByText( sEmployeeId );
        }

        /// <summary>
        /// Formats the List URL.
        /// </summary>
        /// <param name="sReturnOrderStatusId">sReturnOrderStatusId</param>
        /// <returns>string</returns>
        protected string FormatReturnOrderListUrl( string sReturnOrderStatusId )
        {
            string queryString = WebUtility.AddRemoveFromQueryString( Request.QueryString , "orderStatusId" , sReturnOrderStatusId , "orderStatusId" );

            return string.Format( "~/Administration/ReturnOrderList.aspx{0}" , queryString );
        }

        #endregion

        #region Private

        /// <summary>
        /// Load ReturnOrder statuses
        /// </summary>
        private void LoadReturnOrderStatuses()
        {
            List<ReturnOrderStatus> orderStatusCollection = ReturnOrderStatusCache.GetList( true );
            if ( orderStatusCollection.Count > 0 )
            {
                rptReturnOrderStatus.Visible = true;
                rptReturnOrderStatus.DataSource = orderStatusCollection;
                rptReturnOrderStatus.DataBind();
            }
            else
            {
                rptReturnOrderStatus.Visible = false;
            }
        }
      
        /// <summary>
        /// Sets the ReturnOrderList properties.
        /// </summary>
        private void SetProperties()
        {
            this.Title = string.Format( WebUtility.MainTitleTemplate , Master.SiteSettings.SiteName , "退换单列表" );
        }

        /// <summary>
        /// Load ReturnOrders
        /// </summary>
        private void LoadReturnOrders( List<ReturnOrderHeader> orderCollection )
        {
            lblNumberOfTotalReturnOrders.Text = orderCollection.Count.ToString();

            dgReturnOrders.CurrentPageIndex = 0;
            dgReturnOrders.DataSource = orderCollection;

            int nIndex = 0;
            dgReturnOrders.Columns[nIndex++].HeaderText = "登记日期";
            dgReturnOrders.Columns[nIndex++].HeaderText = "退换单明细";
            dgReturnOrders.Columns[nIndex++].HeaderText = "退换单状态";
            dgReturnOrders.Columns[nIndex++].HeaderText = "旺旺ID";
            dgReturnOrders.Columns[nIndex++].HeaderText = "客户姓名";
            dgReturnOrders.Columns[nIndex++].HeaderText = "客户电话";
            dgReturnOrders.Columns[nIndex++].HeaderText = "客户地址";
            dgReturnOrders.Columns[nIndex].HeaderText = "更新人";

            dgReturnOrders.DataBind();
        }

        #endregion

        #endregion
    }
}
